package com.github.message.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class StreamMessageListener implements StreamListener<String, MapRecord<String, String, String>> {

    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        if (message == null) {
            log.info("收到了空消息");
            return;
        }

        try {
            log.info("收到Stream - ID：{}", message.getId());
            log.info("消息内容:{}", message.getValue());
            
            // 处理消息业务逻辑
            processStreamMessage(message.getValue());
            
        } catch (Exception e) {
            System.err.println("处理消息时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void processStreamMessage(Map<String, String> messageData) {

        String messageId = messageData.get("id");
        String content = messageData.get("content");
        String type = messageData.get("type");

        System.out.println("开始处理消息: " + messageId);
        System.out.println("内容: " + content);
        System.out.println("类型: " + type);

        System.out.println("消息处理完成: " + messageId);
    }
}